import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QVBoxLayout
from PyQt5.QtGui import QPixmap, QFont
from PyQt5.QtCore import Qt, QTimer, QDateTime

class OverlayLayoutExample(QWidget):
    def __init__(self):
        super().__init__()
        self.init_ui()
        
    def init_ui(self):
        # 设置窗口标题和大小
        self.setWindowTitle("叠加布局示例")
        self.setGeometry(100, 100, 1200, 100)
        #self.setFixedSize(1500, 300) 
        # 背景图片
        self.bg_label = QLabel(self)
        #self.bg_label.setPixmap(QPixmap('views/assets/header_bg.png').scaled(self.width(), 200, Qt.KeepAspectRatioByExpanding))
        
        self.bg_label.setPixmap(QPixmap("views/assets/header_bg.png"))  # 替换为实际的背景图片路径
        self.bg_label.setScaledContents(True)
        self.bg_label.setAlignment(Qt.AlignCenter)

        # 时间Label
        self.time_label = QLabel(self)
        self.time_label.setFont(QFont("Arial", 12))
        self.time_label.setStyleSheet("""
        QLabel {
            /*background: transparent;   背景透明 */
            color: black;  /* 文本颜色 */
            white;margin-top:20px;margin-left:200px;
        }
    """)
        #self.time_label.setStyleSheet("color: red;")  # 设置文字颜色
        self.time_label.setAlignment(Qt.AlignLeft | Qt.AlignTop)
        self.time_label.setText('时间')
        # 标题图片
        '''
        self.title_label = QLabel(self)
        self.title_label.setPixmap(QPixmap("views/assets/header_title.png"))  # 替换为实际的标题图片路径
        self.title_label.setScaledContents(True)
        self.title_label.setAlignment(Qt.AlignCenter)'''

        # 使用布局叠加
        layout = QVBoxLayout(self)
        layout.addWidget(self.bg_label)

        # 叠加时间和标题
        #self.time_label.raise_()  # 确保时间Label在前面
        #self.title_label.raise_()  # 确保标题图片在前面

        # 设置动态更新时间
        '''
        timer = QTimer(self)
        timer.timeout.connect(self.update_time)
        timer.start(1000)  # 每秒更新一次时间
        self.update_time()'''

    def update_time(self):
        # 动态更新时间
        current_time = QDateTime.currentDateTime().toString("yyyy-MM-dd hh:mm:ss")
        self.time_label.setText(current_time)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = OverlayLayoutExample()
    window.show()
    sys.exit(app.exec_())
